package com.exam.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.ApiResult;
import com.exam.entity.FillQuestion;
import com.exam.mapper.FillQuestionMapper;
import com.exam.service.FillQuestionService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FillQuestionServiceImpl implements FillQuestionService {

    @Resource
    private FillQuestionMapper fillQuestionMapper;

    @Override
    public List<FillQuestion> findByIdAndType(Integer paperId) {
        return fillQuestionMapper.findByIdAndType(paperId);
    }

    @Override
    public IPage<FillQuestion> findAll(Page<FillQuestion> page) {
        return fillQuestionMapper.findAll(page);
    }


    /**
     * 查询最后一条questionId
     * @return
     */
    @Override
    public ApiResult findOnlyQuestionId() {
        FillQuestion res = fillQuestionMapper.findOnlyQuestionId();
        if (res == null) {
            return ApiResultHandler.buildApiResult(500,"查询失败",res);
        }
        return ApiResultHandler.buildApiResult(200,"查询成功",res);

    }

    /**
     * 添加填空题
     * @param fillQuestion
     * @return
     */
    @Override
    public ApiResult add(FillQuestion fillQuestion) {
         int res = fillQuestionMapper.add(fillQuestion);
        if (res < 0) {
            return ApiResultHandler.buildApiResult(400,"添加失败",res);
        }
        return ApiResultHandler.buildApiResult(200,"添加成功",res);
    }

    @Override
    public List<Integer> findBySubject(String subject, Integer pageNo) {
        return fillQuestionMapper.findBySubject(subject,pageNo);
    }
}
